//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2004-2007
//
// Postal address-related helpers.
//

# include "StdAfx.h"
# include "PAHelpers.h"
# include "Map.h"
# include "Globals.h"

void FillCity (HWND _hWnd, int _iCtrlId, const city_t * _pCity, const char * _strDefault) {
	char strCity [256];
	FillCity (strCity, ARRAY_SIZE (strCity), _pCity, _strDefault);
	::SetDlgItemText (_hWnd, _iCtrlId, strCity);
}

void FillCity (char * _strCity, size_t _cLen, const city_t * _pCity, const char * _strDefault) {
	if (_pCity && ! _pCity->IsDeleted ()) {
		string_t strCity (_pCity->strName);
		if (_pCity->pRegion) {
			strCity.append (" (");
			strCity.append (_pCity->pRegion->strAbbrevName.empty () ? _pCity->pRegion->strName : _pCity->pRegion->strAbbrevName);
			if (_pCity->pRegion->pCountry) {
				strCity.append (", ");
				strCity.append (_pCity->pRegion->pCountry->strAbbrevName.empty () ? _pCity->pRegion->pCountry->strName : _pCity->pRegion->pCountry->strAbbrevName);
			}
			strCity.append (")");
		}
		::strcpy (_strCity, strCity.c_str ());
	} else
		::strcpy (_strCity, _strDefault);
}


void FillZip (HWND _hWnd, int _iCtrlId, const zip_t * _pZip, const char * _strDefault) {
	::SetDlgItemText (
		_hWnd, _iCtrlId,
		_pZip && ! _pZip->IsDeleted () ? _pZip->strName.c_str () : _strDefault
	);
}

////////////////////////////////////////////////////////////

# include "COMBase.h"

void SetAutoCompleteForStreet (HWND _hEdit, const city_t * _pCity) {
	typedef std::set<string_t> strings_t;
	strings_t streets;
	{
		CCritSec::CLock lock (g_cs);

		for (int nKind = 0; nKind < rkMax; ++ nKind) {
			const layer_t & layer = g_map.layers [nKind];
			const rgns_t & rgns = layer.rgns;
			for (rgns_t::const_iterator iRgn = rgns.begin (); iRgn != rgns.end (); ++ iRgn) {
				const rgn_t & rgn = * iRgn;
				if (rgn.IsDeleted () || rgn.IsWTR ())
					continue;
				if (! rgn.pExtra)
					continue;

				if (_pCity && rgn.pExtra->pCity != _pCity)
					continue;

				if (! rgn.pExtra->strStreetDesc.empty ())
					streets.insert (rgn.pExtra->strStreetDesc);
			}
		}
	}

	CEnumString<strings_t> * const pEnum = new CEnumString<strings_t> (streets);
	CComPtr<IEnumString> pEnumStrings;
	pEnum->QueryInterface (IID_IEnumString, reinterpret_cast<void **> (& pEnumStrings));

	CComPtr<IAutoComplete> pAC;
	::CoCreateInstance (CLSID_AutoComplete, NULL, CLSCTX_INPROC_SERVER, IID_IAutoComplete, reinterpret_cast<void **> (& pAC));
	CComPtr<IAutoComplete2> pAC2;
	pAC.QueryInterface (& pAC2);

	pAC2->SetOptions (ACO_AUTOSUGGEST | ACO_USETAB | ACO_UPDOWNKEYDROPSLIST);

	pAC->Init (_hEdit, pEnumStrings, NULL, NULL);
}


